package com.example.andoidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends Activity {

   public final static String RESULT_FILE_LIST = "com.example.androidtest.ResultFileList";
   public final static String SEARCH_WORKING_TIME = "com.example.androidtest.SearchWorkingTime";
   
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btnFilterFileNames = (Button) this.findViewById(R.id.btnFilterFileNames);
		btnFilterFileNames.requestFocus();
	}
	
	public void btnFilterFileNamesOnClick(View V){
		EditText txtSearchKeyWord = (EditText) this.findViewById(R.id.txtSearchKeyWord);
		EditText txtRootPaths = (EditText) this.findViewById(R.id.txtRootPaths);
		EditText txtExcludeKeyWord = (EditText) this.findViewById(R.id.txtExcludeKeyWord);
		RadioButton rdbtnSerialSearch = (RadioButton) this.findViewById(R.id.rdbtnSerialSearch);
		RadioButton rdbtnParallelSearch = (RadioButton) this.findViewById(R.id.rdbtnParallelSearch);
		Searcher searcher;

		if (rdbtnSerialSearch.isChecked())
			searcher = new SerialFileSearch();
		else
			searcher = new ParallelFileSearch();
			
		String searchKeyWord = txtSearchKeyWord.getText().toString();
		String[] rootPaths = txtRootPaths.getText().toString().trim().replaceAll("\\r", "").split("\\n");
		String excludeKeyWord = txtExcludeKeyWord.getText().toString();
		
		long startTime = System.currentTimeMillis();
		String result = searcher.getSearchResults(searchKeyWord, rootPaths, excludeKeyWord);
		long finishTime = System.currentTimeMillis();
		long workingTime = finishTime - startTime;

		Intent intent = new Intent (this, SecondActivity.class);
		intent.putExtra(MainActivity.RESULT_FILE_LIST, result);
		intent.putExtra(MainActivity.SEARCH_WORKING_TIME, workingTime);
		this.startActivity(intent);
	}
	
}
